/**
 * 
 */
package org.gas.barapraja.handler.validator;

import org.apache.log4j.Logger;
import org.gas.barapraja.db.dao.AccountDao;
import org.gas.barapraja.db.entity.Account;
import org.gas.barapraja.handler.GasValidatorAdapter;
import org.gas.barapraja.util.GasApplicationContext;

import com.logica.smpp.Data;
import com.logica.smpp.pdu.BindRequest;

/**
 * @author gilang
 *
 */
public class BindRequestValidator extends GasValidatorAdapter {
	
	private static Logger logger=Logger.getLogger(BindRequestValidator.class);
	
	private AccountDao accountDao;
	
	private String systemId;
	
	public BindRequestValidator() {
		accountDao=GasApplicationContext.getAccountDao();
	}

	/* (non-Javadoc)
	 * @see org.gas.barapraja.handler.GasValidatorAdapter#validate(com.logica.smpp.pdu.BindRequest)
	 */
	@Override
	public int validate(BindRequest request) {
		int status=Data.ESME_ROK;
		//ParserFactory factory=ParserUtility.getFactory();
		//Account account=factory.parseAccount(request.getSystemId());
		Account account=accountDao.findBySystemId(request.getSystemId());
		if(account!=null){
			String passwd=account.getPassword();
			if(passwd!=null){
				if(!request.getPassword().equals(passwd)){
					status=Data.ESME_RINVPASWD;
					logger.debug("invalid password for id: "+request.getSystemId());
				}else{
					systemId=request.getSystemId();
					logger.info("system id: "+systemId+" authenticated");
				}
			}else{
				status=Data.ESME_RINVPASWD;
				logger.debug("password not found for id: "+request.getSystemId());
			}
		}else{
			status=Data.ESME_RINVSYSID;
			logger.debug("failed to authenticated id:"+request.getSystemId()+" [user not found]");
		}
		return status;
	}

	/**
	 * @return the systemId
	 */
	public String getSystemId() {
		return systemId;
	}
	
	

}
